package com.example.demo.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Create:Space
 * @Time: 2020-6-10 19:16:00
 * @return 返回当前时间字符串
 */
public class TimeUtils
{

    public static String getStringDate()
    {
        Date date=new Date();
        SimpleDateFormat datetime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return datetime.format(date);
    }


    public static String getFirstDayOfMonth(Integer n)
    {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar=Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH,1 );
        calendar.add(Calendar.MONTH, n);
        return simpleDateFormat.format(calendar.getTime());
    }

    public static String getLastDayOfMonth(Integer n)
    {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar=Calendar.getInstance();
        calendar.set(Calendar.DATE,1 );
        calendar.roll(Calendar.DATE, -1);
        calendar.add(Calendar.MONTH, n);
        return simpleDateFormat.format(calendar.getTime());
    }

    public static void main(String args[])
    {
        System.out.println(TimeUtils.getFirstDayOfMonth(-1));
        System.out.println(TimeUtils.getLastDayOfMonth(-1));
    }
}
